﻿using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web.Mvc;
using WD.Db.Contract;
using WD.Db.Contract.Model;
using WD.Framework.Contract;
using WD.Web.MVC.Common;
using WD.Web.MVC.Controllers.Filters;

namespace WD.Web.MVC.Areas.BaseArea.Controllers
{

    public class Rbac_RoleController : AdminControllerBase
    {
        //
        // GET: /BaseArea/Rbac_Role/
        [AdminOnlineFilter]
        public ActionResult Index(BaseRequest request)
        {
            var result = Rbac_RoleService.GetRbac_RoleList(request);
            return View(result);
        }


        //
        // GET: /BaseArea/Rbac_Role/Create
        [AdminOnlineFilter]
        public ActionResult Create()
        {
            var model = new Rbac_Role();
            return View("Create", model);
        }

        //
        // POST: /BaseArea/Rbac_Role/Create

        [HttpPost]
        [AdminOnlineFilter]
        public string Create(FormCollection collection)
        {
            var ajax = new AjaxResult();
            var model = new Rbac_Role();
            TryUpdateModel(model);
            try
            {
                Rbac_RoleService.SaveRbac_Role(model, LoginInfo.LoginId);
            }
            catch (BusinessException e)
            {
                return ajax.Err(e.Message);
            }
            return ajax.Suc("新增信息成功!");
        }

        //
        // GET: /BaseArea/Rbac_Role/Edit/5
        [AdminOnlineFilter(Url = "/BaseArea/Rbac_Role/Edit")]
        public ActionResult Edit(int id)
        {
            var model = Rbac_RoleService.GetRbac_Role(id);
            return View("Edit", model);
        }

        //
        // POST: /BaseArea/Rbac_Role/Edit/5

        [HttpPost]
        [AdminOnlineFilter(Url = "/BaseArea/Rbac_Role/Edit_Save")]
        public string Edit(int id, FormCollection collection)
        {
            var ajax = new AjaxResult();
            var model = Rbac_RoleService.GetRbac_Role(id);
            TryUpdateModel(model);
            Rbac_RoleService.SaveRbac_Role(model, LoginInfo.LoginId);
            return ajax.Suc("信息修改成功!");
        }

        //
        // GET: /BaseArea/Rbac_Role/Delete/5

        [HttpPost]
        [AdminOnlineFilter]
        public string Deleted(List<int> ids)
        {
            var ajax = new AjaxResult();
            try
            {
                Rbac_RoleService.DeleteRbac_Role(ids, LoginInfo.LoginId);
            }
            catch (BusinessException e)
            {
                return ajax.Err(e.Message);
            }

            return ajax.Suc("信息已成功删除!");
        }


        [AdminOnlineFilter(Url = "/BaseArea/Rbac_Role/SetFunctionModel")]
        public ActionResult SetFunctionModel(int id)
        {
            var re = new RbacFunctionModelRequest { PageSize = 500, ModelType =CommonCode.功能模块类型_系统模块};
            var list = Rbac_FunctionModelService.GetRbac_FunctionModelList(re).OrderBy(x => x.ModelCode).ToList();

            var model = Rbac_RoleService.GetRbac_Role(id);
            var functionModelList = model.Rbac_FunctionModel.ToList();

            var tmpZtreeList = list.Select(x => new ZtreeNode()
                {
                    id = x.ModelID.ToString(CultureInfo.InvariantCulture),
                    pId = x.ParentID.ToString(),
                    name = x.ModelName,
                    open = true,
                    @checked = functionModelList.Any(f => f.ID == x.ID)
                }).ToList();

            var resultList = ToZtreeNode(tmpZtreeList, "");
            ViewBag.Nodes = JsonP(resultList);
            ViewBag.id = id;

            return View();
        }

        [HttpPost]
        [AdminOnlineFilter(Url = "/BaseArea/Rbac_Role/SetFunctionModel")]
        public string SetFunctionModel(int id, string ids)
        {
            var ajax = new AjaxResult();
            var idList = new List<decimal>();
            if (!string.IsNullOrWhiteSpace(ids))
            {
                ids = ids.TrimEnd(',');
                idList = ids.Split(",".ToCharArray()).Select(decimal.Parse).ToList();
            }

            Rbac_RoleService.SetRbac_RoleFunctionModelList(id, idList);
            return ajax.Suc("角色功能配置成功!");
        }
    }
}
